mui.init({
	beforeback: function () {
		//获得列表界面的webview
		var parentOobj = plus.webview.currentWebview().opener();
		mui.fire(parentOobj, 'updateDataList');
		return true;
	}
});
var wxChannel = null; // 微信支付  
var aliChannel = null; // 支付宝支付  
var channel = null;   //支付通道 

mui.plusReady(function () {
	var oid = plus.webview.currentWebview().oid;

	//获取支付通道
	plus.payment.getChannels(function(channels){
		for (var i in channels) {
			if (channels[i].id == "wxpay") {
				wxChannel = channels[i];
			}else {
				aliChannel = channels[i];
			}
		}
	},function(e){
		alert("获取支付通道失败: "+e.message);
	});
	
	order_detail = new Vue({
		el: '#order_detail',
		data: {
			orderData:[],
			pay_methods: 'wxpay',
		},
		mounted(){
			var self = this;
			self.getOrderDetails();
		},
		methods: {
			getOrderDetails: function () {
				var self = this;
				var api_host = plus.storage.getItem('HOST_URL');
				mui.ajax({
					url: api_host + '/api/user/user/myOrderDetails',
					dataType: 'json',
					headers: {'User-Token': plus.storage.getItem('UserToken')},
					data: {'id': oid},
					type: 'get',
					success: function (data) {
						if(data.code == 1){
							self.orderData = data.data;
						}else{
							mui.toast(data.msg, {duration: '1000',type: 'div'});
						}
					},
					error: function (xhr, type, errorThrown) {
						mui.toast('订单获取错误，请返回重试', {duration: '1000',type: 'div'});
					}
				})
			},
			pay_page: function () {
				$('.popMask').show();
				$('.popPay').animate({
					'bottom': '0%'
				}, 300).show();
			},
			closePay: function () {
				$('.popMask').hide();
				$('.popPay').animate({
					'bottom': '-100%'
				}, 300).hide(300);
			},
			pay_way: function (e) {
				this.pay_methods = e;
			},
			show_pay: function () {
				$('.popMask').show();
				$('.gwDialog').animate({
					'top': "50%"
				}, 300).show();
			},
			cancle_pay: function () {
				$('.popMask').hide();
				$('.gwDialog').animate({
					'top': "150%"
				}, 300).hide(300);
			},
			confirm_pay: function (order_num) {
				var self = this;
				var api_host = plus.storage.getItem('HOST_URL');
				mui.ajax({
					url: api_host + '/api/user/User/cancleOrder',
					dataType: 'json',
					data: {
						'order_num': self.orderData.order_num
					},
					type: 'post',
					async: true,
					timeout: 1000,
					success: function (data) {
						mui.toast(data.msg, {duration: '1000',type: 'div'});
					},
					error: function (xhr, type, errorThrown) {
						console.log("系统错误，请稍后重试");
					}
				})
				$('.popMask').hide();
				$('.gwDialog').animate({
					'top': "150%"
				}, 300).hide();
				mui.back();
			},

			pay: function (id) {
				var self = this;
				// 从服务器请求支付订单
				var PAYSERVER = '';

				var api_host = plus.storage.getItem('HOST_URL');
				var ALIPAYSERVER = api_host+'/api/pay/alipay/pay_again';
				var WXPAYSERVER = api_host + '/api/pay/wxpay/pay_again';

				if(id == 'alipay'){
			        PAYSERVER = ALIPAYSERVER;
			        channel = aliChannel;
			    }else if(id == 'wxpay'){
			        PAYSERVER = WXPAYSERVER;
			        channel = wxChannel;
			    }else{
			        plus.nativeUI.alert("不支持的支付类型哦！",null,"提示");
			        return;
			    }


				mui.ajax({
					url: PAYSERVER,
					dataType: 'json',
					data: {
						'order_num': self.orderData.order_num,
					},
					type: 'GET',
					async: true,
					timeout: 1000,
					success: function (data) {
						plus.payment.request(channel, data, function (result) {
							plus.nativeUI.alert("支付成功！", function () {
								mui.toast('支付成功', {duration: '1000',type: 'div'});
								$('.popMask').hide();
								$('.popPay').animate({
									'bottom': '-100%'
								}, 300).hide(300);
								self.paymentSuccess();
							});
						}, function (e) {
							mui.toast('支付失败咯，您需要重新发起支付哦', {
								duration: '2000',
								type: 'div'
							});
						});
					},
					error: function (xhr, type, errorThrown) {
						console.log(JSON.stringify(errorThrown));
						console.log('系统错误！')
					}
				});
			},

			paymentSuccess: function () {
				var self = this;
				var api_host = plus.storage.getItem('HOST_URL');
				mui.ajax({
					url: api_host + '/api/pay/wxpay/updata_pay_agagin',
					dataType: 'json',
					data: {
						'order_num': self.orderData.order_num
					},
					type: 'post',
					async: true,
					timeout: 1000,
					success: function (data) {
						setTimeout(function(){
							mui.openWindow({
								url:'myOrder.html',
								createNew:false,
								show:{
									autoShow:false
								}
							});
						},1000)
					},
					error: function (xhr, type, errorThrown) {
						console.log('系统错误！')
					}
				});
			}
		}
	})
})